
data$forcing = as.integer(as.Date("1990-11-04"))- as.integer(as.Date(data$dob))
data$Cutoff08 = ifelse(data$forcing >= 0, 1, 0)



################# DV: Vote 2016 ########################

ivmod1_white = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_white",
                                            downstream_voted = "vote.gen2016_white", downstream_lag = "lag2016_white", bandwidth = bw, state.abbrev=NULL), silent = T)
ivmod1_black = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_black",
                             downstream_voted = "vote.gen2016_black", downstream_lag = "lag2016_black", bandwidth = bw, state.abbrev=NULL), silent = T)
ivmod1_hispanic = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_hispanic",
                                            downstream_voted = "vote.gen2016_hispanic", downstream_lag = "lag2016_hispanic", bandwidth = bw, state.abbrev=NULL), silent = T)



##################### DV: Vote 2014 ##########################

 ivmod2_white = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_white", 
                                             downstream_voted = "vote.gen2014_white", downstream_lag = "lag2014_white", bandwidth = bw, state.abbrev=NULL), silent = T)
 ivmod2_black = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_black", 
                                             downstream_voted = "vote.gen2014_black", downstream_lag = "lag2014_black", bandwidth = bw, state.abbrev=NULL), silent = T)
 ivmod2_hispanic = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_hispanic", 
                                                downstream_voted = "vote.gen2014_hispanic", downstream_lag = "lag2014_hispanic", bandwidth = bw, state.abbrev=NULL), silent = T)

##################### DV: Vote 2012 ##########################
 ivmod3_white = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_white", 
                                             downstream_voted = "vote.gen2012_white", downstream_lag = "lag2012_white", bandwidth = bw, state.abbrev=NULL), silent = T)
 ivmod3_black = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_black", 
                                             downstream_voted = "vote.gen2012_black", downstream_lag = "lag2012_black", bandwidth = bw, state.abbrev=NULL), silent = T)
 ivmod3_hispanic = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_hispanic", 
                                                downstream_voted = "vote.gen2012_hispanic", downstream_lag = "lag2012_hispanic", bandwidth = bw, state.abbrev=NULL), silent = T)


##################### DV: Vote 2010 ##########################


ivmod4_white = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_white", 
                                            downstream_voted = "vote.gen2010_white", downstream_lag = "lag2010_white", bandwidth = bw, state.abbrev=NULL), silent = T)
ivmod4_black = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_black", 
                                            downstream_voted = "vote.gen2010_black", downstream_lag = "lag2010_black", bandwidth = bw, state.abbrev=NULL), silent = T)
ivmod4_hispanic = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_hispanic", 
                                               downstream_voted = "vote.gen2010_hispanic", downstream_lag = "lag2010_hispanic", bandwidth = bw, state.abbrev=NULL), silent = T)

# ##################### DV: Democrat ##########################
 ivmod5_white = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_white", 
                                             downstream_voted = "democrat_white", downstream_lag = "lagdem_white", bandwidth = bw, state.abbrev=NULL), silent = T)
 ivmod5_black = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_black", 
                                             downstream_voted = "democrat_black", downstream_lag = "lagdem_black", bandwidth = bw, state.abbrev=NULL), silent = T)
 ivmod5_hispanic = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_hispanic", 
                                                downstream_voted = "democrat_hispanic", downstream_lag = "lagdem_hispanic", bandwidth = bw, state.abbrev=NULL), silent = T)

# ##################### DV: Republican ##########################
 ivmod_rep_white = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_white", 
                                             downstream_voted = "republican_white", downstream_lag = "lagrep_white", bandwidth = bw, state.abbrev=NULL), silent = T)
 ivmod_rep_black = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_black", 
                                             downstream_voted = "republican_black", downstream_lag = "lagrep_black", bandwidth = bw, state.abbrev=NULL), silent = T)
 ivmod_rep_hispanic = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_hispanic", 
                                                downstream_voted = "republican_hispanic", downstream_lag = "lagrep_hispanic", bandwidth = bw, state.abbrev=NULL), silent = T)

# ##################### Registered ##########################
 ivmod6_white = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_white", 
                                             downstream_voted = "white", downstream_lag = "lagregistered_white", bandwidth = bw, state.abbrev=NULL), silent = T)
 ivmod6_black = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_black", 
                                             downstream_voted = "black", downstream_lag = "lagregistered_black", bandwidth = bw, state.abbrev=NULL), silent = T)
 ivmod6_hispanic = try(state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_hispanic", 
                                                downstream_voted = "hispanic", downstream_lag = "lagregistered_hispanic", bandwidth = bw, state.abbrev=NULL), silent = T)



rm(data)
filename = paste0("results_", state, "_bw", bw,".Rdata")
save.image(paste0("results/", filename))

rm(list=ls(pattern = "mod"))
